package org.example.struct.array;

import com.alibaba.fastjson.JSON;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author liyishan
 * @date 2023/8/1 10:28
 * @apiNote
 */

public class Test00268MissingNumber {
    public static void main(String[] args) {
        int[] a = new int[]{1, -1, 1, -1, 1, -1,1};
        int count = test(a);
        System.out.println(count + "  array:" + JSON.toJSON(a));
    }


    public static int test(int[] nums) {
        Arrays.sort(nums);
        for(int i = 0;i <nums.length;i++){
            if(nums[i] != i){
                return i;
            }
        }
        return nums.length;
    }

    public static int test1(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        for (int i = 0; i < nums.length+1; i++) {
            if(set.add(i)){
                return i;
            }
        }
        return -1;
    }

    public static int test2(int[] nums) {
        int n = nums.length;
        int total = n *(n +1) / 2;
        int sum = 0;
        for(int i=0;i<nums.length;i++){
            sum += nums[i];
        }
        return total - sum;
    }
}
